﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Krile.Forms.ConfigForm.Children.Controls
{
    public partial class PairColorConfigure : UserControl
    {
        public Color ForeColorValue
        {
            get { return ColorButton.ForeColor; }
            set { ColorButton.ForeColor = value; }
        }

        public Color BackColorValue
        {
            get { return ColorButton.BackColor; }
            set { ColorButton.BackColor = value; }
        }

        public bool TreatForeEnabled
        {
            get { return foreEnabledSwitch.Visible; }
            set
            {
                foreEnabledSwitch.Checked = true;
                foreEnabledSwitch.Visible = value;
            }
        }

        public bool TreatBackEnabled
        {
            get { return backEnabledSwitch.Visible; }
            set
            {
                backEnabledSwitch.Checked = true;
                backEnabledSwitch.Visible = value;
            }
        }

        public bool ForeColorEnabled
        {
            get { return foreEnabledSwitch.Checked; }
            set
            {
                foreEnabledSwitch.Checked = value;
                foreEnabledSwitch_CheckedChanged(null, null);
            }
        }

        public bool BackColorEnabled
        {
            get { return backEnabledSwitch.Checked; }
            set
            {
                backEnabledSwitch.Checked = value;
                backEnabledSwitch_CheckedChanged(null, null);
            }
        }

        public PairColorConfigure(string desc, Color fore, Color back)
        {
            InitializeComponent();
            Description.Text = desc;
            ForeColorValue = fore;
            BackColorValue = back;
            TreatForeEnabled = false;
            TreatBackEnabled = false;
        }

        public PairColorConfigure(string desc, Color fore, Color back, bool foreEnabled, bool backEnabled)
        {
            InitializeComponent();
            Description.Text = desc;
            ForeColorValue = fore;
            BackColorValue = back;
            TreatForeEnabled = true;
            TreatBackEnabled = true;
            ForeColorEnabled = foreEnabled;
            BackColorEnabled = backEnabled;
        }

        private void ColorButton_Click(object sender, EventArgs e)
        {
            colorSelectContext.Show(ColorButton, new Point(0, ColorButton.Height));
        }

        private void selectForeground_Click(object sender, EventArgs e)
        {
            Color clr;
            if (SelectColor(this.ForeColorValue, out clr))
                this.ForeColorValue = clr;
        }

        private void selectBackground_Click(object sender, EventArgs e)
        {
            Color clr;
            if (SelectColor(this.BackColorValue, out clr))
                this.BackColorValue = clr;
        }

        private bool SelectColor(Color init, out Color after)
        {
            using (var cd = new ColorDialog())
            {
                cd.AllowFullOpen = true;
                cd.AnyColor = true;
                cd.Color = init;
                if (cd.ShowDialog() == DialogResult.OK)
                {
                    after = cd.Color;
                    return true;
                }
                else
                {
                    after = Color.Empty;
                    return false;
                }
            }
        }

        private void backEnabledSwitch_CheckedChanged(object sender, EventArgs e)
        {
            selectBackground.Enabled  = backEnabledSwitch.Checked;
        }

        private void foreEnabledSwitch_CheckedChanged(object sender, EventArgs e)
        {
            selectForeground.Enabled = foreEnabledSwitch.Checked;
        }

    }
}
